package paixu;


import java.util.Arrays;

/**
 * @author lj
 * @Description: 选择排序
 * @date 2020-10-29 14:59
 * @QQ 851477108
 */


public class xuanze {
    public static void main(String[] args){
        int[] arr = {5,8,6,2,4,3,11,12,10,40,15,90,55};

        int[] sort = selection(arr);
        for (int value : sort) {
            System.out.print(value+",");
        }
        System.out.println(Arrays.toString(arr));

    }

    public static int[] selection(int[] arr){
        if (arr == null || arr.length < 2){
            return arr;
        }
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
/*
                if (arr[j]<arr[minIndex]){
                    minIndex = j;
                }
*/
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            swap(arr,i,minIndex);
        }
        return arr;
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
